package com.cloud.workTest;

import com.cloud.bean.*;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
        HelloInterface helloInterface = new Hello();
        InvocationHandler handler = new MyInvocationHandler(helloInterface);
        HelloInterface proxyHello = (HelloInterface) Proxy.newProxyInstance(helloInterface.getClass().getClassLoader(),
                helloInterface.getClass().getInterfaces(), handler);
        proxyHello.greeting();

//        未实现接口会直接报错：
//        Exception in thread "main" java.lang.IllegalArgumentException: com.cloud.bean.Entity is not an interface
//        可使用CGLib动态代理
//        Entity entity = new Entity("id", "shuchuanbiao");
//        InvocationHandler handler1 = new MyInvocationHandler(entity);
//        Entity proxy = (Entity) Proxy.newProxyInstance(Entity.class.getClassLoader(), new Class[]{Entity.class}, handler1);
//        proxy.toString();
    }
}
